package com.example.mateproject.util;


import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.TypedValue;
import android.widget.ImageView;

import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.example.mateproject.R;

import java.io.File;
import java.util.concurrent.ExecutionException;

public class GlideUtil {
    public static void loadCornerImageWithoutPlaceHolder(ImageView imageView, String filePath, RequestListener listener, float radius) {
        RoundedCorners transform = null;
        if ((int) radius > 0) {
            transform = new RoundedCorners((int) radius);
        }

        RequestOptions options = new RequestOptions().centerCrop();
        if (transform != null) {
            options = options.transform(transform);
        }
        Glide.with(Utils.getApp()).load(filePath).apply(options).listener(listener).into(imageView);
    }

    public static void loadCornerImageWithPlaceHolder(ImageView imageView, String filePath,int placeholder,int fallback, RequestListener listener, float radius) {
        RoundedCorners transform = null;
        if ((int) radius > 0) {
            transform = new RoundedCorners((int) radius);
        }

        RequestOptions options = new RequestOptions().centerCrop();
        if (transform != null) {
            options = options.transform(transform);
        }
        Glide.with(Utils.getApp()).load(filePath).placeholder(placeholder).fallback(fallback).apply(options).listener(listener).into(imageView);
    }

    public static void clear(ImageView imageView) {
        Glide.with(Utils.getApp()).clear(imageView);
    }

    public static void loadImage(ImageView imageView, String filePath, RequestListener listener) {
        Glide.with(Utils.getApp())
            .load(filePath)
            .listener(listener)
            .apply(new RequestOptions().error(getAttrResId(Utils.getApp(), R.drawable.ic_load_failed)))
            .into(imageView);
    }

    public static void loadImage(ImageView imageView, String filePath) {
        Glide.with(Utils.getApp())
            .load(filePath)
            .apply(new RequestOptions().error(getAttrResId(Utils.getApp(), R.drawable.ic_load_failed)))
            .into(imageView);
    }

    public static void loadImage(ImageView imageView, Uri uri) {
        if (uri == null) {
            return;
        }
        Glide.with(Utils.getApp())
            .load(uri)
            .apply(new RequestOptions().error(getAttrResId(Utils.getApp(), R.drawable.ic_load_failed)))
            .into(imageView);
    }

    public static void loadImage(String filePath, String url) {
        try {
            File file = Glide.with(Utils.getApp()).asFile().load(url).submit().get();
            File destFile = new File(filePath);
            file.renameTo(destFile);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    public void loadImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
        Glide.with(context).load(uri).apply(new RequestOptions().override(resizeX, resizeY).priority(Priority.HIGH).fitCenter()).into(imageView);
    }

    public static void loadImage(ImageView imageView, Object uri) {
        if (uri == null) {
            return;
        }
        Glide.with(Utils.getApp())
            .load(uri)
            .apply(new RequestOptions().error(getAttrResId(Utils.getApp(), R.drawable.ic_load_failed)))
            .into(imageView);
    }

    public static void loadUserIcon(ImageView imageView, Object uri) {
        loadUserIcon(imageView, uri, 0);
    }

    public static void loadUserIcon(ImageView imageView, Object uri, int radius) {
        Glide.with(Utils.getApp())
            .load(uri)
            .placeholder(getAttrResId(Utils.getApp(), R.drawable.ic_loading))
            .apply(new RequestOptions().centerCrop().error(getAttrResId(Utils.getApp(), R.drawable.ic_load_failed)))
            .into(imageView);
    }

    public static void loadUserIcon(ImageView imageView, Object uri, int defaultResId, int radius) {
        Glide.with(Utils.getApp()).load(uri).placeholder(defaultResId).apply(new RequestOptions().centerCrop().error(defaultResId)).into(imageView);
    }

    public static Bitmap loadBitmap(Object imageUrl, int targetImageSize) throws InterruptedException, ExecutionException {
        if (imageUrl == null) {
            return null;
        }
        return Glide.with(Utils.getApp())
            .asBitmap()
            .load(imageUrl)
            .apply(new RequestOptions().error(getAttrResId(Utils.getApp(), R.drawable.ic_load_failed)))
            .into(targetImageSize, targetImageSize)
            .get();
    }

    public static Bitmap loadBitmap(Object imageUrl, int width, int height) throws InterruptedException, ExecutionException {
        if (imageUrl == null) {
            return null;
        }
        return Glide.with(Utils.getApp())
            .asBitmap()
            .load(imageUrl)
            .apply(new RequestOptions().error(getAttrResId(Utils.getApp(), R.drawable.ic_load_failed)))
            .into(width, height)
            .get();
    }

    public static void loadImageSetDefault(ImageView imageView, Object uri, int defaultResId) {
        Glide.with(Utils.getApp()).load(uri).placeholder(defaultResId).apply(new RequestOptions().centerCrop().error(defaultResId)).into(imageView);
    }

    public static int getAttrResId(Context context, int attrId) {
        if (context == null || attrId == 0) {
            return 0;
        }
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(attrId, typedValue, true);
        return typedValue.resourceId;
    }
}
